package com.zjht.gmcc.bean.file;

import java.io.BufferedReader;
import java.io.File;
import java.io.FileFilter;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilenameFilter;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.io.OutputStreamWriter;
import java.util.Comparator;
import java.util.Map;

import com.zjht.gmcc.bean.util.MyFormat;

/**
 * 文件操作相关方法
 * @author dyh 2010-08-16
 */
public class FileUtil implements java.io.Serializable {
	private static final long serialVersionUID = 1923158838657051800L;

	public static final int SQL_MAXROW = 2000000;// 从页面上提取数据的最大行数

	public static class CompratorByLastModified implements Comparator {
		public int compare(Object o1, Object o2) {
			File file1 = (File) o1;
			File file2 = (File) o2;
			long diff = file1.lastModified() - file2.lastModified();
			if (diff > 0)
				return -1;
			else if (diff == 0)
				return 0;
			else
				return 1;
		}

		public boolean equals(Object obj) {
			return true; // 简单做法
		}
	}

	public static class CompratorBySize implements Comparator {
		public int compare(Object o1, Object o2) {
			File file1 = (File) o1;
			File file2 = (File) o2;
			long diff = file1.length() - file2.length();
			if (diff > 0)
				return 1;
			else if (diff == 0)
				return 0;
			else
				return -1;
		}

		public boolean equals(Object obj) {
			return true; // 简单做法
		}
	}

	/**
	 * 取得指定扩展名的文件列表
	 * @param extension
	 * @return
	 */
	public static FilenameFilter getFileExtensionFilter(String extension) {
		final String _extension = extension;
		return new FilenameFilter() {
			public boolean accept(File file, String name) {
				boolean ret = name.endsWith(_extension);
				return ret;
			}
		};
	}

	/**
	 * 取得文件名满足所指定的规则表达式的文件列表
	 * @param regex
	 * @return
	 */
	public static FilenameFilter getFileRegexFilter(String regex) {
		final String regex_ = regex;
		return new FilenameFilter() {
			public boolean accept(File file, String name) {
				boolean ret = name.matches(regex_);
				return ret;
			}
		};
	}

	/**
	 * 取得非目录的文件列表
	 * @return
	 */
	public static FileFilter getNotDirFileFilter() {
		return new FileFilter() {
			public boolean accept(File file) {
				return file.isFile();
			}
		};
	}

	/**
	 * 复制文件
	 * @author dyh 2010-08-07
	 * @param sourcefile 源文件
	 * @param targetfile 目标文件
	 * @return 是否成功复制文件
	 */
	public static boolean copyFile(String sourcefilepath, String targetfilepath) {
		File sourcefile = null;
		try {
			int bytesum = 0;
			int byteread = 0;
			sourcefile = new File(sourcefilepath);
			if (sourcefile.exists()) { // 文件存在时
				InputStream inStream = new FileInputStream(sourcefilepath); // 读入原文件
				FileOutputStream fs = new FileOutputStream(targetfilepath);
				byte[] buffer = new byte[1444];
				while ((byteread = inStream.read(buffer)) != -1) {
					bytesum += byteread; // 字节数 文件大小
					fs.write(buffer, 0, byteread);
				}
				inStream.close();
				return true;
			} else
				return false;

		} catch (Exception ex) {
			return false;
		} finally {
			try {
				if (sourcefile != null) {
					sourcefile = null;
				}
			} catch (Exception ex) {
			} finally {

			}
		}
	}

	/**
	 * 转移文件
	 * @author dyh 2010-08-07
	 * @param sourcefile 源文件
	 * @param targetfile 目标文件
	 * @return 是否成功转移文件
	 */
	public static boolean transferFile(String sourcefilepath, String targetfilepath) {
		File sourcefile = null;
		try {
			int bytesum = 0;
			int byteread = 0;
			sourcefile = new File(sourcefilepath);
			if (sourcefile.exists()) { // 文件存在时
				InputStream inStream = new FileInputStream(sourcefilepath); // 读入原文件
				FileOutputStream fs = new FileOutputStream(targetfilepath);
				byte[] buffer = new byte[1444];
				while ((byteread = inStream.read(buffer)) != -1) {
					bytesum += byteread; // 字节数 文件大小
					fs.write(buffer, 0, byteread);
				}
				inStream.close();
				sourcefile.delete();
				return true;
			} else
				return false;

		} catch (Exception ex) {
			return false;
		} finally {
			try {
				if (sourcefile != null) {
					sourcefile = null;
				}
			} catch (Exception ex) {
			} finally {

			}
		}
	}

	/**
	 * 读取文件内容到StringBuilder中
	 * @author dyh 2010-8-15
	 * @param buffer
	 * @param filepath
	 * @return
	 */
	public static boolean read2Buffer(StringBuilder buffer, String filepath) {
		return read2Buffer(buffer, filepath, null);
	}

	/**
	 * 读取文件内容到StringBuilder中
	 * @author dyh 2010-8-15
	 * @param buffer
	 * @param filepath
	 * @param encoding 字符编码
	 * @return
	 */
	public static boolean read2Buffer(StringBuilder buffer, String filepath, String encoding) {
		if (buffer == null)
			return false;
		InputStream is = null;
		try {
			is = new FileInputStream(filepath);
			String line; // 用来保存每行读取的内容
			BufferedReader reader = null;
			if (MyFormat.isStrNull(encoding))
				reader = new BufferedReader(new InputStreamReader(is));
			else
				reader = new BufferedReader(new InputStreamReader(is, encoding));
			while ((line = reader.readLine()) != null) { // 如果 line 为空说明读完了
				buffer.append(line); // 将读到的内容添加到 buffer 中
			}
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		} finally {
			try {
				is.close();
				is = null;
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return true;
	}

	/**
	 * 将StringBuilder内容写到文件
	 * @author dyh 2010-9-15
	 * @param buffer
	 * @param filepath
	 * @return
	 */
	public static boolean buffer2File(StringBuilder buffer, String filepath) {
		return buffer2File(buffer, filepath, null);
	}

	/**
	 * 将StringBuilder内容写到文件
	 * @author dyh 2010-9-15
	 * @param buffer
	 * @param filepath
	 * @param encoding 字符编码
	 * @return
	 */
	public static boolean buffer2File(StringBuilder buffer, String filepath, String encoding) {
		File newFile = null;
		OutputStreamWriter fw = null;
		try {
			newFile = new File(filepath);
			if (MyFormat.isStrNull(encoding))
				fw = new OutputStreamWriter(new FileOutputStream(newFile));
			else
				fw = new OutputStreamWriter(new FileOutputStream(newFile), encoding);
			fw.write(buffer.toString());
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		} finally {
			try {
				newFile = null;
				fw.close();
				fw = null;
			} catch (IOException e) {
				e.printStackTrace();
			}
			buffer = null;
		}
		return true;
	}

	/**
	 * 读取文件内容到Map中
	 * @author dyh 2011-02-05
	 * @param map
	 * @param filepath
	 * @return
	 */
	public static boolean read2Map(Map<String, String> map, String filepath) {
		if (map == null)
			return false;
		InputStream is = null;
		try {
			is = new FileInputStream(filepath);
			String line; // 用来保存每行读取的内容
			BufferedReader reader = null;
			reader = new BufferedReader(new InputStreamReader(is, "UTF-8"));
			int index = 0;
			String key = "";
			String value = "";
			while ((line = reader.readLine()) != null) { // 如果 line 为空说明读完了
				line = line.trim();
				if (line.startsWith("//") || line.startsWith("/*") || line.endsWith("*/"))// 跳过注释
					continue;
				index = line.indexOf("=");
				if (index == -1 || index == 0)
					continue;
				key = line.substring(0, index);
				value = line.substring(index + 1);
				key = key.trim();
				value = value.trim();
				if (MyFormat.isStrNull(key) || MyFormat.isStrNull(value)) {
					continue;
				}
				map.put(key, value); // 将读到的内容添加到 map 中
			}
		} catch (IOException e) {
			e.printStackTrace();
			return false;
		} finally {
			try {
				is.close();
				is = null;
			} catch (IOException e) {
				e.printStackTrace();
			}
		}
		return true;
	}
}